package com.longxi.tianjintianbao.ui.fragment.houseinspection;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.ui.adapter.RuZhuYanFangPagerAdapter;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.base.LazyFragment;

import java.util.List;

import butterknife.BindView;

/**
 * Created by cxw on & 2018/9/19
 */
public class RuZhuYanFangFragment extends LazyFragment {
    @BindView(R.id.tabLayout)
    SegmentTabLayout mTabLayout;
    @BindView(R.id.vp)
    ViewPager mViewPager;

    private String type;
    private String[] mTitles;
//    private String[] mTitles_1 = {"待验房", "已验房", "验房中", "已验房"};
//    private String[] mTitles_2 = {"全部", "未验房", "验房中", "已验房"};
//    private String[] mTitles_3 = {"全部", "未恢复", "已恢复"};
    private String[] mTitles_1 = {"待验房", "已验房"};
    private String[] mTitles_2 = {"待验房", "已验房"};
    private String[] mTitles_3 = {"待验房", "已验房"};

    private RuZhuYanFangPagerAdapter ruZhuYanFangPagerAdapter;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_yanfang;
    }

    @Override
    protected void init(View view) {
        type = getArguments().getString("state");
        initRuZhuYanFang();
        initViewPager();
    }

    /**
     * 初始化tab数据
     */
    private void initRuZhuYanFang() {
        switch (type) {
            case AppConst.YAN_FANG_RUZHU:
                mTitles = mTitles_1;
                break;
            case AppConst.YAN_FANG_TUIZHU:
                mTitles = mTitles_2;
                break;
            case AppConst.YAN_FANG_ZICHAN:
                mTitles = mTitles_3;
                break;
        }
        mTabLayout.setTabData(mTitles);
    }

    public void initViewPager(){
        ruZhuYanFangPagerAdapter = new RuZhuYanFangPagerAdapter(getChildFragmentManager(), mTitles, type);
        mViewPager.setAdapter(ruZhuYanFangPagerAdapter);
        mViewPager.setOffscreenPageLimit(mTitles.length - 1);
        mViewPager.setCurrentItem(0);
    }

    public RuZhuYanFangFragment setArguments(String state) {
        Bundle bundle = new Bundle();
        bundle.putString("state", state);
        setArguments(bundle);
        return this;
    }

    @Override
    public void initListener() {
        super.initListener();
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
